package com.dataset.manager;

import com.dataset.manager.model.Dataset;
import com.dataset.manager.service.DatasetService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
public class DatasetServiceTest {
    
    @Autowired
    private DatasetService datasetService;
    
    @Test
    public void testCreateDataset() throws Exception {
        // 添加sourceType和sourcePath参数
        CompletableFuture<Dataset> future = datasetService.createDataset("Test Dataset", "A test dataset", "local", "");
        assertNotNull(future);
        
        // 等待异步操作完成
        Dataset dataset = future.get(); // 使用get()而不是join()以正确处理异常
        assertNotNull(dataset);
        assertEquals("Test Dataset", dataset.getName());
        assertEquals("A test dataset", dataset.getDescription());
        assertEquals(Dataset.Status.COMPLETED, dataset.getStatus());
        assertEquals("local", dataset.getSourceType());
    }
    
    @Test
    public void testListDatasets() {
        List<Dataset> datasets = datasetService.listDatasets();
        assertNotNull(datasets);
        // 由于我们已经在DatasetServiceImpl中通过@PostConstruct添加了一个示例数据集，所以列表不应为空
        assertFalse(datasets.isEmpty());
        assertEquals(1, datasets.size());
    }
    
    @Test
    public void testDeleteDataset() throws Exception {
        // 先创建一个数据集，添加sourceType和sourcePath参数
        CompletableFuture<Dataset> future = datasetService.createDataset("To Delete", "Dataset to delete", "local", "");
        Dataset dataset = future.get();
        
        // 删除数据集
        boolean deleted = datasetService.deleteDataset(dataset.getId());
        assertTrue(deleted);
        
        // 确认数据集已被删除
        Dataset deletedDataset = datasetService.getDatasetById(dataset.getId());
        assertNull(deletedDataset);
        
        // 确认列表大小回到初始状态
        List<Dataset> datasets = datasetService.listDatasets();
        assertEquals(1, datasets.size());
    }
}